﻿@{
    Layout = null;
}
@using CNKI.TPI.Web.Search.Model;
@using CNKI.TPI.Web.Base;
<script type="text/javascript">
    function configValue(fieldName) {
        $(".ullist").empty();
        $("#IndexName").val(fieldName)//当前行的字段名
        opendbsearch();

    }
    function closedbsearch() {
        $(".talkDivBg").hide();
        $(".setdbsearch").hide();
    }
    function opendbsearch() {
        $(".talkDivBg").show();
        $(".setdbsearch").show();
    }
    //下拉列表变化事件
    function tableselectchange(e) {
        //2代表下拉框
        var trobj = e.parentNode.parentNode;
        var tdobj = $(trobj).find("td");
        var fieldName = $.trim(tdobj.eq(0).html());
        if (tdobj.eq(2).find("select").val() == "@((int)SubmitFieldType.ComboBox)") {
            tdobj.eq(3).children('span').remove()
            tdobj.eq(3).append("<span class=\"f-help\"><a onclick=\"configValue('" + fieldName + "')\">配置</a></span>");
        }
        else {
            tdobj.eq(3).children('span').remove();
        }
    }
    //删除样式
    function delStyle() {
        var len = $('#tableField tbody tr').size();
        if (len > 0) {
            confirmDialog('确定要删除吗?', function () {
                $.ajax({
                    type: "POST",
                    url: "@Html.Raw(Url.Action("StyleDel", "Admin_Submit"))",
                data: { "dbcode": "@ViewBag.DbCode" },
                success: function (m) {
                    $("#tableField tbody").empty();
                    $(".wrapR-c-box-b-right ul").empty();
                },
                error: function (e) {
                    messageAlert("操作失败！");
                }
            })
        })
    }
    else {
        messageAlert("列表暂无数据，不支持删除！");
    }
}
//创建样式
function createStyle() {
    var len = $(".wrapR-c-box-b-right ul li label").size();
    if (len > 0) {
        delOldStyle();
        //循环选择的字段
        $(".wrapR-c-box-b-right ul li label").each(function () {
            var thistext = $.trim($(this).text());
            var trnew = existField(thistext);
            var valueString = $(this).find("input").val();
            var displayname = thistext;
            var defalutvlaue = "";
            var isqueird = "";
            var isqueirdenable = "";
            var isblank = "";
            var isblankenable = "";
            arraystr = valueString.split(',');
            if (arraystr.length > 0) {
                if (arraystr[1] != "" && arraystr[1] != "undefined") {
                    displayname = arraystr[1];
                }
                if (arraystr[2] != "" && arraystr[2] != "undefined") {
                    defalutvlaue = arraystr[2];
                }
                if (arraystr[3] != "" && arraystr[3] == "1") {  //唯一
                    isqueird = "checked=checked";
                    isqueirdenable = "disabled=disabled";
                }
                if (arraystr[4] != "" && arraystr[4] == "0") {  //不允许为空
                    isblank = "checked=checked";
                    isblankenable = "disabled=disabled";
                }
            }
            if (trnew == "") {
                //插入新记录
                trnew = "<tr><td>" + thistext + "</td><td><input type=\"text\" onblur=\"onBlurText(this)\" value=" + displayname + " class=\"f-text span2\" maxlength=\"200\"></td>" +
                  "<td class=\"tc\"> <select class=\"f-select span2\" onchange=\"tableselectchange(this)\">" + $("#SelectList").val() + "</select></td>" +
                "<td><input onblur=\"onBlurText(this)\" type=\"text\" value='" + defalutvlaue + "' class=\"f-text span2\" maxlength=\"200\"></td>" +
                "<td class=\"tc\"> <input type=\"checkbox\" class=\"f-checkbox\" " + isblank + " " + isblankenable + "></td>" +
                "<td class=\"tc\"><input type=\"checkbox\" class=\"f-checkbox\" " + isqueird + " " + isqueirdenable + "></td>" +
                "<td class=\"tc\"> <input type=\"text\" value=\"\" onblur=\"onBlurText(this)\" class=\"f-text span3\" maxlength=\"200\"> </td></tr>"
            }
            $("#tableField tbody").append(trnew);
        })
        messageAlert("生成完成！");
    }
    else {
        messageAlert("请先选择提交字段！");
    }
}
//判断字段是否存在，存在则返回已有tr，且从table移除，不存在则返回空
function existField(fieldtext) {
    var trtext = "";
    $("#tableField tbody tr").each(function () {
        var thistr = $(this);
        if ($.trim(thistr.find("td").eq(0).text()) == $.trim(fieldtext)) {
            trtext = $(thistr);
            thistr.remove();
            return trtext;
        }
    })
    return trtext;
}
//先删除table里面与已选择字段不匹配的tr
function delOldStyle() {
    $("#tableField tbody tr").each(function () {
        var thistr = $(this);//行
        var m = 0;
        $(".wrapR-c-box-b-right ul li label").each(function () {
            var thistext = $(this).text();
            if ($.trim(thistr.find("td").eq(0).text()) == $.trim(thistext)) {
                m = 1;
            }
        })
        if (m == 0) {
            thistr.remove();
        }
    })
}
//保存
function saveFieldList() {
    disablesave();

    var len = $('#tableField tbody tr').size();
    if (len <= 0) {
        messageAlert("请先生成提交样式！");
        enablesave();
        return;
    }
    //循环table保存
    var jsonfield = [];
    var flage = true;
    $('#tableField tbody tr').each(function () {
        var tdobj = $(this).find("td");
        var disname = $.trim(tdobj.eq(1).find("input").val());
        if (disname != "") {
            if (!regtext(disname)) {
                flage = false;
            }
        }
        var defalutval = $.trim(tdobj.eq(3).find("input").val());
        if (defalutval != "") {
            if (!regtextsql(defalutval)) {
                flage = false;
            }
        }
        var instrustval = $.trim(tdobj.eq(6).find("input").val());
        if (instrustval != "") {
            if (!regtext(instrustval)) {
                flage = false;
            }
        }
        var data = {
            "NAME": "@ViewBag.UserName",
                "DBNAME": "@ViewBag.DbName",
                "DBCODE": "@ViewBag.DbCode",
                "FIELDNAME": $.trim(tdobj.eq(0).html()),
                "FIELDDISPLAYNAME": disname,
                "FIELDTYPE": tdobj.eq(2).find("select").val(),
                "DEFAULTVALUE": defalutval,
                "ISALLOWNULL": tdobj.eq(4).find("input").attr("checked") == "checked" ? 1 : 0,
                "ISONLYONE": tdobj.eq(5).find("input").attr("checked") == "checked" ? 1 : 0,
                "DESCRIPT": instrustval,
                "TYPE": "1",
                "SORTNUM": len
            }
            len = len - 1;
            jsonfield.push(data);
        })
        var datastr = { "list": jsonfield };
        if (flage) {
            $.ajax({
                type: "POST",
                url: "@Html.Raw(Url.Action("StyleSave", "Admin_Submit"))",
                contentType: "application/json",
                data: JSON.stringify(datastr),
                success: function (m) {
                    if (m == "True") {
                        messageAlert("操作成功！");
                    }
                    else {
                        messageAlert("操作失败！");
                    }
                    enablesave();
                },
                error: function (e) {
                    messageAlert("操作失败！");
                    enablesave();
                }
            })
        }
        else {
            messageAlert("请检查输入项是否有特殊字符！");
            enablesave();

        }
    }
    function disablesave() {
        $(".btn-warn-f").attr("disabled", "disabled");
    }
    function enablesave() {
        $(".btn-warn-f").removeAttr("disabled", "disabled");
    }
    //文本框验证特殊字符
    function onBlurText(e) {
        var value = $(e).val();
        if (value != "") {
            if (!regtext(value)) {
                messageAlert("不允许输入特殊字符！");
            }
        }
    }
    function onBlurTextSql(e) {
        var value = $(e).val();
        if (value != "") {
            if (!regtextsql(value)) {
                messageAlert("不允许输入特殊字符！");
            }
        }
    }
</script>
<!-- wrapRStart -->
<h2>数据库：@ViewBag.DbName</h2>
<h3 class='box-h'>提交样式设置</h3>
<div class="wrapR-c wrapR-c2" style="margin-top: 20px">
    <div class="clearfix posr wrapR-c2-in">
        <div class="wrapR-c-box wrapR-c-l fl posr" style="overflow-y: visible">
            <div class="wrapR-c-box-t">可提交字段</div>
            <div class="wrapR-c-box-b-left" style="overflow-y: scroll; height: 380px">
                <ul>

                    @if (ViewBag.ListField != null)
                    {
                        foreach (SYS_CMS_DATABASE_FIELD item in ViewBag.ListField)
                        {
                            if (!item.Field.Trim().StartsWith("SYS_FLD_"))
                            {
                                string valuestr = item.Field + "," + item.Caption + "," + item.DefaultValue + "," + item.FieldValueUnique + "," + item.NullEnable;
                              
                        <li class="f-ct">
                            <label class="f-label block">
                                <input type="checkbox" class="f-checkboxL" onclick="leftcheck(this)" value='@valuestr' />@item.Field</label>

                        </li>
                            }
                        }
                    }
                </ul>
                <div class="f-ct wrapR-c-box-footer posa">
                    <label class="f-label ml10">
                        <input type="checkbox" class="f-checkbox-left" onclick="leftCheckAll(this)" />全选</label>
                </div>
            </div>

        </div>
        <div class="wrapR-c-m tc posa"><a class="iconfont-s iconfont-right-triangle-s" onclick="rightMove('.wrapR-c-box-b-left','.wrapR-c-box-b-right')"></a></div>
        <div class="fr posr">
            <div class='wrapR-c-box wrapR-c-r wrapR-c-r-l' style="overflow-y: visible">
                <div class="wrapR-c-box-t">选择的提交字段</div>
                <div class="wrapR-c-box-b-right" style="overflow-y: scroll; height: 380px">
                    <ul class="ulfield">
                        @if (ViewBag.SubmitList != null)//修改时加载已经设置的字段
                        {
                            foreach (SYS_CMS_SUBMIT item in ViewBag.SubmitList)
                            {
                            <li class="f-ct">
                                <label class="f-label block right">
                                    <input type="checkbox" class="f-checkboxR" onclick="inputclickright(this)" value='@item.FIELDNAME' />@item.FIELDNAME</label>

                            </li>
                            }
                        }
                    </ul>
                    <div class="f-ct wrapR-c-box-footer posa clearfix">
                        <label class="f-label ml10 fl">
                            <input type="checkbox" class="f-checkbox-right" onclick="rightCheckAll(this)">全选</label>
                        <a class="btn btn-s btn-warn-f fr deleteright" onclick="deleteright()">删除</a>
                    </div>
                </div>
            </div>

            <div class="wrapR-c-r-r form-h posa" style="top: 10px">
                <a title="移至最顶部" onclick=" movefirst(this)" class="iconfont-s iconfont-upest-s"></a>
                <br />
                <a title="上移" onclick="moveablove(this)" class="iconfont-s iconfont-up-s"></a>
                <br />
                <a title="下移" onclick="movedown(this)" class="iconfont-s iconfont-down-s"></a>
                <br />
                <a title="移至最底部" onclick="movelast(this)" class="iconfont-s iconfont-downest-s"></a>

            </div>
            <div class="posa" style="bottom: 0; right: -105px;">
                <a onclick="createStyle()" class="btn btn-m btn-prim-f mb10">生成样式</a><br />
                <a onclick="delStyle();" class="btn btn-m btn-defa-f">删除样式</a>
            </div>
        </div>


    </div>
    <table id="tableField" width="100%" cellspacing="0" cellpadding="0" class="table table-wr" style="margin-top: 20px;">
        <thead>
            <tr class="thead">
                <th>字段</th>
                <th>显示名</th>
                <th>类型</th>
                <th>默认值</th>
                <th>非空</th>
                <th>唯一</th>
                <th>说明</th>
            </tr>
        </thead>
        <tbody>
            @if (ViewBag.SubmitList != null)//修改时加载已经设置的字段
            {
                foreach (SYS_CMS_SUBMIT item in ViewBag.SubmitList)
                {
                    string strblank = "";
                    string strquird = "";
                <tr>
                    <td>@item.FIELDNAME</td>
                    <td>
                        <input type="text" value="@item.FIELDDISPLAYNAME" onblur="onBlurText(this)" class="f-text span2" maxlength="200"></td>
                    <td class="tc">
                        <select class="f-select span2" onchange="tableselectchange(this)">
                            @foreach (SubmitFieldType sel in ViewBag.SelectIList)
                            {
                                if (item.FIELDTYPE == ((int)sel))
                                {
                                <option value=" @((int)sel)" selected="selected">@StringHelper.GetSubmitFieldTypeDesc(sel)</option>
                                }
                                else
                                {
                                <option value=" @((int)sel)">@StringHelper.GetSubmitFieldTypeDesc(sel)</option>
                                }

                            }
                        </select>
                    </td>
                    <td>
                        <input type="text" value="@item.DEFAULTVALUE" class="f-text span2" onblur="onBlurTextSql(this)" maxlength="200">
                        @if (item.FIELDTYPE == ((int)SubmitFieldType.ComboBox))
                        {
                            <span class="f-help"><a onclick="configValue('@item.FIELDNAME')">配置</a></span>
                        }
                    </td>

                    <td class="tc">
                        @if (item.IsBlank == 0)//CMC设置为非空则不允许修改
                        {
                            strblank = "disabled=disabled";
                        }
                        @if (item.ISALLOWNULL == 1)
                        {
                            <input type="checkbox" checked="checked" @strblank class="f-checkbox">
                        }
                        else
                        {
                            <input type="checkbox" class="f-checkbox">
                        }
                    </td>
                    <td class="tc">
                        @if (item.IsRequired == 1)//CMC设置为唯一则不允许修改
                        {
                            strquird = "disabled=disabled";
                        }
                        @if (item.ISONLYONE == 1)
                        {
                            <input type="checkbox" checked="checked" @strquird class="f-checkbox">
                        }
                        else
                        {
                            <input type="checkbox" class="f-checkbox">
                        }
                    </td>
                    <td class="tc">
                        <input type="text" value="@item.DESCRIPT" onblur="onBlurText(this)" class="f-text span3" maxlength="200">
                    </td>
                </tr>
                }
            }
        </tbody>
    </table>

    <div class="tc mt30" style="padding-bottom: 30px">
        <button class="btn btn-m btn-submit btn-warn-f" onclick="saveFieldList()">保存</button>
        <a class="btn btn-m btn-prim-f" href="javascript:void(0);" onclick="ajaxLoad('initload','@Html.Raw(Url.Action("SubmitPage", "Admin_Submit"))')">返回</a>
    </div>
    <input type="hidden" id="SelectFlag" name="SelectFlag" value="@ViewBag.SelectFlag" />
    <input type="hidden" id="SelectList" name="SelectList" value="@ViewBag.SubmitSelect" />
    <input type="hidden" id="IndexName" name="IndexName" />
</div>
<!-- wrapREnd -->



<script>
    $(function () {
        dbchange();

    })
    function radioSelect(m) {
        if (m == 1) {
            $("#valuefieldList").hide();
            $("#valueInput").val("");
            $("#valueInput").show();
        }
        else {
            $("#valuefieldList").show();
            $("#valueInput").hide();
        }
    }
    //数据发生变化时，绑定检索，查询字段列表
    function dbchange() {
        var dbcode = $("#dbList").val();
        //清空表达式
        $(".ullist").empty();
        $.ajax({
            type: "GET",
            url: timestamp("@Html.Raw(Url.Action("GetDbField", "Admin_Submit"))"),
            data: { "DbCode": dbcode },
            success: function (msg) {
                var obj = eval(msg);
                $("#searchFieldList").empty();
                $("#wherefieldList").empty();
                if (obj != null && obj.length > 0) {
                    for (var i = 0; i < obj.length; i++) {
                        var name = obj[i];
                        if (name.indexOf("SYS_") < 0) {
                            var listr = " <OPTION>" + name + "</OPTION>";
                            $("#wherefieldList").append(listr);
                            $("#searchFieldList").append(listr);
                        }
                    }
                }
            },
            error: function (e)
            { }

        })
    }
    //添加where条件
    function addItem() {

        var dbcode = $("#dbList").val();
        var dbtablename = getdbtablename(); //数据库名
        if (dbtablename == "") {
            messageAlert("请选择要检索的数据库！");
            return;
        }
        var wherefield = $("#wherefieldList").val();//where条件
        if (wherefield == "") {
            messageAlert("请选择检索条件配置字段！");
            return;
        }
        var oper = $("#operateFlage").val();//操作符
        var inputtext = "";//检索值
        ;
        if ($("input[name='racgroup']:checked").val() == 0) {
            inputtext = $.trim($("#valuefieldList").find("option:selected").text());
        }
        else {
            inputtext = $("#valueInput").val();
        }
        if (inputtext == "") {
            messageAlert("请选择或录入检索值！");
            return;
        }
        var unitflage = $("#unitFlage").val();//连接符

        var iteminfo = "";
        iteminfo = "(" + dbtablename + "." + wherefield + oper + inputtext + ")" + unitflage;
        var listr = " <li class=\"f-ct\" style=\"margin-left:5px\">  <label class=\"f-label block\" style=\"margin-left:5px\">" +
                      " <input type=\"checkbox\" class=\"f-checkbox\" value=" + iteminfo + " />" + iteminfo + "</label></li>";
        $(".ullist").append(listr);
    }
    //生成sql
    function saveSql() {
        var ullist = $(".ullist li");
        if (ullist.size() <= 0) {
            messageAlert("请先添加表达式！");
            return;
        }
        var dbtablename = getdbtablename(); //数据库名
        if (dbtablename == "") {
            messageAlert("请选择要检索的数据库！");
            return;
        }
        var searchfield = $("#searchFieldList").val();//查询字段
        if (searchfield == "") {
            messageAlert("请选择要检索的字段！");
            return;
        }
        var wherestr = ""; //where 条件
        ullist.each(function () {
            wherestr += $.trim($(this).find("input").val());
        })
        wherestr = $.trim(wherestr);
        var len = wherestr.length;   //去除最后一位 AND OR
        if (wherestr.substring(len - 3, len).toUpperCase() == "AND") {
            wherestr = wherestr.substring(0, len - 3);
        }
        if (wherestr.substring(len - 2, len).toUpperCase() == "OR") {
            wherestr = wherestr.substring(0, len - 2);
        }
        var sqlstr = "select " + searchfield + " from " + dbtablename + " where " + wherestr + " group by " + searchfield + " order by " + searchfield;

        var thisFiled = $("#IndexName").val();
        $('#tableField tbody tr').each(function () {
            var tdobj = $(this).find("td");
            var disname = $.trim(tdobj.eq(0).html());
            if (disname == thisFiled) {
                tdobj.eq(3).find("input").val(sqlstr);
            }
        })
        closedbsearch();
    }
    //获取数据库名称
    function getdbtablename() {
        var dbcode = $("#dbList").val();
        var dbtablename = "";
        if (dbcode == "SYS_CMS_USER") {
            dbtablename = dbcode;
        }
        else {
            dbtablename = dbcode + "_METADATA";
        }
        return dbtablename;
    }
    function del() {
        var delobj = $('.ullist input:checked');
        if (delobj.size() <= 0) {
            messageAlert("请选择要删除的表达式！");
            return;
        }
        else {
            $('.ullist input:checked').each(function () {
                $(this).parent().parent().remove();//找到li元素移除
            })
        }
    }
</script>
<!--透明背景-->
<div class="talkDivBg" style="display: none; z-index: 999">

    <!--talkDiv弹出层(不带图片，宽550)-->
    <div class="talkDiv talkW-800 form-h setdbsearch" style="display: none; margin-top: -289px; width: 690px;">
        <div class="talkDiv-title">
            <span>检索数据库配置</span>
            <a class="icon icon-close" title="关闭" href="javascript:void(0);" onclick="closedbsearch()"><i class="iconfont icon-error3"></i></a>
        </div>
        <div class="talkDiv-body talkDiv-body1">
            <div class="clearfix pt10 border-bottom-solid mb10">
                <div class="f-gp fl">
                    <div class="f-lb">
                        <label class="f-label">检索数据库：</label>
                    </div>
                    <div class="f-ct">
                        <select class="f-select span3" id="dbList" onchange="dbchange()">
                            @{if (ViewBag.DBList != null)
                          {
                              foreach (SYS_CMS_DATABASE item in ViewBag.DBList)
                              {
                                  if (item.SearchType == ((int)SearchType.TPI))
                                  {
                                            <option value="@item.DatabaseCode">@item.DatabaseName</option>
                                  }
                              }
                                    <option value="SYS_CMS_USER">SYS_CMS_USER</option>

                          }
                            }
                        </select>
                    </div>
                </div>
                <div class="f-gp fr">
                    <div class="f-lb">
                        <label class="f-label">检索字段：</label>
                    </div>
                    <div class="f-ct">
                        <select class="f-select span3" id="searchFieldList"></select>
                    </div>
                </div>
            </div>

            <div class="clearfix mb10">
                <div class="f-gp fl">
                    <div class="f-lb">
                        <label class="f-label">字段名称：</label>
                    </div>
                    <div class="f-ct">
                        <select class="f-select span3" id="wherefieldList"></select>
                    </div>
                </div>
                <div class="f-gp fr">
                    <div class="f-lb">
                        <label class="f-label">操作符：</label>
                    </div>
                    <div class="f-ct">
                        <select class="f-select span3" id="operateFlage">
                            <option>=</option>
                            <option>></option>
                            <option><</option>
                            <option>>=</option>
                            <option><=</option>

                        </select>
                    </div>
                </div>
            </div>
            <div class="f-gp mb20">
                <div class="f-lb">
                    <label class="f-label">检索值：</label>
                </div>
                <div class="f-ct">
                    <label class="f-label">
                        <input type="radio" name="racgroup" value="0" checked="checked" onclick="radioSelect(0)" />选择已有字段
                    </label>
                    <label class="f-label">
                        <input type="radio" name="racgroup" value="1" onclick="radioSelect(1)" />手动输入
                    </label>

                    <select class="f-select span3" id="valuefieldList">
                        @{if (ViewBag.listField != null)
                      {
                                <option value="*">*</option>
                          foreach (SYS_CMS_DATABASE_FIELD item in ViewBag.listField)
                          {
                              if (!item.Field.StartsWith("SYS_"))
                              {
                                        <option value="@item.Field">###@item.Field</option>
                              }
                          }
                      }
                        }
                    </select>
                    <input type="text" id="valueInput" value="" onblur="onBlurText(this)" class="f-text span3" maxlength="20" style="display: none">
                </div>
            </div>
            <div class="f-gp mb20">
                <div class="f-lb">
                    <label class="f-label">连接符：</label>
                </div>
                <div class="f-ct">
                    <select class="f-select span3" style="width: 80px" id="unitFlage">
                        <option>AND</option>
                        <option>OR</option>
                    </select>
                    <button class="btn btn-m btn-defa-f btn-submit" onclick="addItem()">添加</button>
                </div>
            </div>
            <div class="f-gp mb20">
                <div class="f-lb">
                    <label class="f-label">表达式：</label>
                </div>
                <div class="f-ct">
                    <ul style="width: 450px; height: 150px; border: 1px solid #e6e6e6; overflow: auto;" class="ullist inline-block"></ul>
                    <a class="btn btn-m btn-prim-f inline-block vab mb8" onclick="del()">删除</a>
                </div>

            </div>
            <div class="tc">
                <a href="javascript:void(0);" class="btn btn-m btn-submit btn-submit btn-defa-f" onclick="saveSql()">确定</a>
                &#12288;<a href="javascript:void(0);" class="btn btn-m btn-submit btn-submit btn-defa-f" onclick="closedbsearch()">取消</a>
            </div>
        </div>


    </div>

</div>